package com.deere.manufacture.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.deere.action.BaseAction;
import com.deere.common.Utils;
import com.deere.dao.GenericDao;
import com.deere.manufacture.service.MRPService;
import com.deere.manufacture.service.ProductionService;
import com.deere.model.GenericPart;
import com.deere.model.Inventory;
import com.deere.model.MRPModel;
import com.deere.model.ProductionOrderItem;
import com.deere.model.WorkOrder;
import com.deere.model.dto.PartDto;
import com.deere.model.dto.ProductionDto;
import com.deere.model.dto.ProductionOrderDto;
import com.deere.model.dto.SalesOrderDto;
/**
 * 生产计划管理Action
 * @author 
 *
 */
public class ProductionAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6282733080587433476L;
	@Autowired
	private ProductionService proService;

	@Autowired
	private MRPService mprService;
	
	@Autowired
	private GenericDao<Inventory> inventoryDao;

	private String dataJson;
	
	private String partCode;
	
	private String partType;
	/**
	 * 工作单
	 */
	private WorkOrder workOrder;
	/*
	 * 
	 */
	private Integer itemId;
	
	private List<WorkOrder> workOrders;

	private List<ProductionDto> productionList = new ArrayList<ProductionDto>();
	/**
	 * 
	 */
	private List<ProductionOrderDto> productionOrderDtos ;
	
	private List<String> orderNumList = Collections.EMPTY_LIST;

	private List<SalesOrderDto> orderList = new ArrayList<SalesOrderDto>();

	private String selectedOrder;
	
	private String order;

	public String listOrderNum() throws Exception {
		this.setOrderNumList(mprService.getUnplannedOrder());
		return SUCCESS;
	}

	/*
	 * 
	 * List unplanned production requirements from mrp<mrpModel> pool
	 * (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {

		// TODO Auto-generated method stub

		System.out.println(selectedOrder);
		this.setProductionList(proService.productionPlan(selectedOrder));

		// this.setProductionList(proService.genProOrder(partList));
		return SUCCESS;
	}

	public String insertProOrder() throws Exception {

		String a = this.getSelectedOrder();
		List<ProductionDto> proList = Utils.json2Object(jsonData, ProductionDto.class);
		proService.generatePlan(proList, selectedOrder);
		return "ppsuccess";
	}
	
	public String calMRP() throws Exception {
		String a = this.getOrder();
		List<PartDto> dtoList = Utils.json2Object(dataJson, PartDto.class);
		Map<String, Integer> mrpList = mprService.getMRP(dtoList);
		Iterator<String> it = mrpList.keySet().iterator();
		while (it.hasNext()) {
			ProductionDto proDto = new ProductionDto();
			String partCode = it.next();
			proDto.setPartCode(partCode);
			proDto.setRequiredQty(mrpList.get(partCode));
			Inventory inv =inventoryDao.findById(proDto.getPartCode());
			if(inv==null) 
				proDto.setInventoryQty(0);
			else 
				proDto.setInventoryQty(inv.getQuantity());
			
			Integer recommandedQty =  proDto.getRequiredQty()-proDto.getInventoryQty();
			if(recommandedQty<0)
				recommandedQty=0;
			proDto.setRecommandedQty(recommandedQty);
			proDto.setActualQty(recommandedQty);
			productionList.add(proDto);
		}
		return "listMRP";
	}
	
	public String listOrder() throws Exception {
		System.out.println("test");
		List<MRPModel> mprList = mprService.listOrderforPart(this.getPartCode());
		for (MRPModel mprModel : mprList) {
			SalesOrderDto soDto = new SalesOrderDto();
			soDto.setSalesOrder(mprModel.getSalesOrder().getOrderNum());
			soDto.setQuantity(mprModel.getRequiredQty());
			soDto.setPartCode(mprModel.getPart().toString());
			soDto.setDeliveryDate(mprModel.getDeliveryDate());
			soDto.setDueDate(mprModel.getDueDate());
			this.getOrderList().add(soDto);
		}
		return "listOrder";
	}
	/**
	 * 查询部分已经排产的POI
	 * @return
	 * @throws Exception
	 */
	public String listPlannedPOItems() throws Exception {
		GenericPart part = new GenericPart();
		part.setPartCode(partCode);
		part.setPartType(partType);
		List<ProductionOrderItem> items = proService.listPlannedPOItems(part);
		productionOrderDtos = new ArrayList<ProductionOrderDto>();
		for(ProductionOrderItem item : items){
			ProductionOrderDto dto  = new ProductionOrderDto();
			BeanUtils.copyProperties(item, dto);
			BeanUtils.copyProperties(item.getProOrder(), dto);
			productionOrderDtos.add(dto);
		}
		return "listPlannedPOItems";
	}
	/**
	 * 查询已经完成的PI的
	 * @return
	 * @throws Exception
	 */
	/*public String listCompletedPOItems() throws Exception {
		GenericPart part = new GenericPart();
		//part.setPartCode("1");
		List<ProductionOrderItem> items = proService.listCompletedPOItems(part);
		productionOrderDtos = new ArrayList<ProductionOrderDto>();
		for(ProductionOrderItem item : items){
			ProductionOrderDto dto  = new ProductionOrderDto();
			BeanUtils.copyProperties(item, dto);
			BeanUtils.copyProperties(item.getProOrder(), dto);
			productionOrderDtos.add(dto);
		}
		return "listPlannedPOItems";
	}*/
	/**
	 * 显示所有还需要继续排产的生产计划单,即末完成的生产计划单.
	 * 主要是用于创建工作单时显示数据
	 * @return
	 * @throws Exception
	 */
	public String listNoCompletedPOItems() throws Exception {
		GenericPart part = new GenericPart();
		part.setPartCode(partCode);
		part.setPartType(partType);
		List<ProductionOrderItem> items = proService.listNoCompletedPOIs(part);
		productionOrderDtos = new ArrayList<ProductionOrderDto>();
		for(ProductionOrderItem item : items){
			ProductionOrderDto dto  = new ProductionOrderDto();
			BeanUtils.copyProperties(item, dto);
			BeanUtils.copyProperties(item.getProOrder(), dto);
			productionOrderDtos.add(dto);
		}
		return "listPlannedPOItems";
	}
	/**
	 * 根据<code>itemId</code>检索对应的工作单
	 * @return
	 * @throws Exception
	 * @see #itemId
	 */
	public String queryWorkOrderseByItemId() throws Exception {
		 ProductionOrderItem item = proService.getPlannedProductionItem(itemId);
		 if(null != item){
			 Set<WorkOrder> workOrderSet = item.getWorkOrdrers();
			 workOrders = new ArrayList<WorkOrder>();
			 workOrders.addAll(workOrderSet);
			 System.out.println(workOrders.size());
		 }
		return "queryWorkOrderseByItemId";
	}

	/************************getter&setter***************************/
	
	public List<ProductionOrderDto> getProductionOrderDtos() {
		return productionOrderDtos;
	}

	public void setProductionOrderDtos(List<ProductionOrderDto> productionOrderDtos) {
		this.productionOrderDtos = productionOrderDtos;
	}

	public Integer getItemId() {
		return itemId;
	}
	
	public List<WorkOrder> getWorkOrders() {
		return workOrders;
	}

	public void setWorkOrders(List<WorkOrder> workOrders) {
		this.workOrders = workOrders;
	}

	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}
	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getDataJson() {
		return dataJson;
	}

	public void setDataJson(String dataJson) {
		this.dataJson = dataJson;
	}

	public List<ProductionDto> getProductionList() {
		return productionList;
	}

	public void setProductionList(List<ProductionDto> productionList) {
		this.productionList = productionList;
	}

	public List<SalesOrderDto> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<SalesOrderDto> orderList) {
		this.orderList = orderList;
	}

	public String getPartCode() {
		return partCode;
	}

	public void setPartCode(String partCode) {
		this.partCode = partCode;
	}
	public WorkOrder getWorkOrder() {
		return workOrder;
	}

	public void setWorkOrder(WorkOrder workOrder) {
		this.workOrder = workOrder;
	}

	public String getSelectedOrder() {
		return selectedOrder;
	}

	public void setSelectedOrder(String selectedOrder) {
		this.selectedOrder = selectedOrder;
	}

	public List<String> getOrderNumList() {
		return orderNumList;
	}

	public void setOrderNumList(List<String> orderNumList) {
		this.orderNumList = orderNumList;
	}

	public String getPartType() {
		return partType;
	}

	public void setPartType(String partType) {
		this.partType = partType;
	}
}
